package com.my.study.aqs.alternate;

import java.util.concurrent.locks.LockSupport;

/**
 * @author Carlos
 * @version 1.0.0
 * @date 2023/2/12 20:14
 */
public class ParkUnParkTest {
    
    static Thread a;
    static Thread b;
    static Thread c;

    public static void main(String[] args) {
        ParkUnPark parkUnPark = new ParkUnPark(5);

        a = new Thread(() -> parkUnPark.print("a", b));
        b = new Thread(() -> parkUnPark.print("b", c));
        c = new Thread(() -> parkUnPark.print("c", a));

        
        a.start();
        b.start();
        c.start();
        
        LockSupport.unpark(a);
    }
    
}


class ParkUnPark {
    private int loopNum;

    public ParkUnPark(int loopNum) {
        this.loopNum = loopNum;
    }
    
    public void print(String str, Thread next) {
        for (int i = 0; i < loopNum; i++) {
            LockSupport.park();
            System.out.print(str);
            LockSupport.unpark(next);
        }
    }
}